import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import userList from '../components/user/userList.vue'
import addUser from '../components/user/addUser.vue' 
import Rights from '../components/power/rights.vue'
import Roles from '../components/power/roles.vue'


Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login', component: Login
    },
    {
      path: '/home', component: Home,
      redirect: '/welcome',
      children : [
        {path: '/welcome', component: welcome},
        {path: '/findAllUser',component:userList},
        {path: '/addUser/:id',component: addUser},
        {path: '/addUser',component: addUser},
        {path: '/rights',component: Rights},
        {path: '/roles',component: Roles}
      ]
    }
  ]
})


//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
    //to将要访问的路径
    //from 代表从哪个路径跳转过来
    //next放行 next('/login')强制放行
    if(to.path === '/login') return next()

    const token = window.sessionStorage.getItem('token')
    //如果token的值为空就让他强制跳转到登录页
    if(!token) return next('/login')

    next()

})

export default router
